//初始化
$(function () {
    fetchData();

    $("#btnSearch").click(function () {
        $('#tb').bootstrapTable("refresh",{
            pageNumber:1
        });
    });
});

function fetchData() {
    $('#tb').bootstrapTable({
        method:'get',
        url:'/customer/Search',
        pagination:true,
        pageNumber:1,
        height:500,
        pageSize:2,
        pageList:[2,10,20,30,50,100],
        sidePagination:"server",
        queryParamsType:"page",
        queryParams:function (params) {
            params["customerName"] = $("#customerName").val();
            params["customerPerson"] = $("#customerPerson").val();
            params["customerTel"] = $("#customerTel").val();
            return params;
        },
        clickToSelect:true,
        columns:[
            {
                title:'客户编号',
                field:'customerId'
            },
            {
                title:'客户公司名称',
                field:'customerName'
            },
            {
                title:'客户联系人',
                field:'customerPerson'
            },
            {
                title:'联系人电话',
                field:'customerTel'
            },
            {
                title:'联系人邮箱',
                field:'customerEmail'
            },
            {
                title:'联系人地址',
                field:'customerAddress'
            },
            {
                title:'操作',
                field:'customerId',
                formatter:optionTemplate
            },
        ],
        responseHandler: function (res) {
            if (res.code === 1) {
                res = res.data;
            }
            return res;
        }
    });
}

function optionTemplate(value,row,index) {
    let str = `<a class="btn btn-primary" onclick='editMethod(${index})' >修改</a>  <a class="btn btn-danger" onclick='deleteMethod(${index})'>删除</a>`;
    return str;
}

function editMethod(index) {
    console.log("编辑");
    let arr = $('#tb').bootstrapTable('getData');
    let data = arr[index];
    location.href="/customer/update?customerId="+data.customerId;
    console.log(data);
}

function deleteMethod(index) {
    console.log("删除");
    let arr = $('#tb').bootstrapTable('getData');
    let data = arr[index];

    if (confirm("你确定要删除这个数据吗?")){
        $.ajax({
            type:"get",
            url:"/customer/delete",
            data:"id=" + data.customerId,
            dataType:"json",
            success:function (resp) {
                if (resp.code===1){
                    alert(resp.data);
                    $("#btnSearch").trigger("click");
                }else {
                    alert(resp.data);
                }
            }
        });
    }
}
